#!/bin/bash


if [[ $# < 1 ]];then
    echo; echo "Usage: $0 pod_keywords <namespace>"; echo;
    exit 1
fi

KEYWORDS=$1

NAMESPACE=${2:-__NAMESPACE}

KUBESERVER=__KUBE_MASTER

RC=$(kubectl --server=${KUBESERVER} get rc --namespace=${NAMESPACE} | grep -i ${KEYWORDS} | cut -d' ' -f1)
RCNUM=$(kubectl --server=${KUBESERVER} get rc --namespace=${NAMESPACE} | grep -i ${KEYWORDS} | cut -d' ' -f1 | wc -l)



PS3='Choose to delete options(input "q" to exit and "all" for delete all)> '

select rc in ${RC};do

    if [[ "$REPLY" =~ ^[0-9]+$ ]] && [ "$REPLY" -ge 1 -a "$REPLY" -le $RCNUM ];then
        echo -en "##########\\033[1;31m BEGIN \\033[0;39m##########";echo
        kubectl --server=${KUBESERVER} delete rc $rc --namespace=${NAMESPACE} 

    elif [ "$REPLY" == 'all' ];then
        echo -en "##########\\033[1;31m BEGIN \\033[0;39m##########";echo
        for rc in $RC;do
            kubectl --server=${KUBESERVER} delete rc $rc --namespace=${NAMESPACE} 
        done

    elif [ "$REPLY" == 'q' ];then
        break

    else
        echo -en "##########\\033[1;31m BEGIN \\033[0;39m##########";echo
        echo 'Input error'
    fi

    REPLY=''
    echo;echo -en "##########\\033[1;31m  END  \\033[0;39m##########";echo
done
